
<%@page import="org.biojava.dasobert.das.Capabilities"%>
<%@page import="org.biojava.dasobert.das.DasSpec"%><jsp:useBean id="bean"     class="dasregistry.updateBean"   scope="session"/>
<jsp:setProperty name="bean" property="*"/>
<jsp:useBean id="register" class="dasregistry.registryBean" scope="session"/> 
<jsp:useBean id="updater"  class="dasregistry.updateBean"   scope="session"/> 
<jsp:useBean id="coord"    class="dasregistry.coordBean"    scope="session"/>

<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasSource"/>
<jsp:directive.page import="org.biojava.dasobert.dasregistry.Das1Source"/>
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasCoordinateSystem"/>
<jsp:directive.page import="org.biojava.services.das.registry.DasClient"/>
<jsp:directive.page import="org.biojava.services.das.registry.DasRegistrySql"/>
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasCoordSysComparator"/>
<jsp:directive.page import="java.util.Comparator"/>
<jsp:directive.page import="java.lang.Integer"/>
<jsp:directive.page import="java.util.Date"/>
<jsp:directive.page import="java.util.Map"/>
<jsp:directive.page import="java.text.SimpleDateFormat"/>


<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.registry.UserManager" %>
<%@ page import="org.biojava.services.das.dao.Role" %>
<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.dao.ProjectBean"%>
<%@ page import="java.util.List"%>
<jsp:directive.page import="org.springframework.beans.factory.BeanFactory"/>
<jsp:directive.page import="org.springframework.context.support.ClassPathXmlApplicationContext"/>


<%@ include file="logincheck.jsp" %>

<%

//fetch by auto id
String auto_id = request.getParameter("auto_id");
DasSource ds = register.getDasSource(auto_id);
if ( ds == null ) {
	ds = new Das1Source();
}
String passkey   = request.getParameter("passkey");
%>


<% String title="edit server";
   String header="Edit Server";
   BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
   DasRegistrySql dasRegistrySql=(DasRegistrySql)ctx.getBean("registryBean");
    
%>

<!-- the sanger default header -->
<%@ include file="sangerheader.jsp" %>

<div id="main">

<jsp:include page="openwindowscript.jsp" />

<%

boolean isOwner = false;
int status = -1 ;

if (coord.userOwnsASource(user,new DasSource[]{ds})) { 
	isOwner = true;
	
} 

if ( Role.isAdmin(user) ) {
	isOwner = true;
}

%>

<% 
if ( !isOwner ) {
  if ( passkey == null ) {
  %>
  <h2>Edit server <%=ds.getNickname()%></h2>
  <%@ include file="sangertablestart.jsp" %>

  <table border="0">
  <tr>
  <td><p> To remove a server, the key is required that is
  returned after registration of the server. </p>
  <p>If you forgot your key, you
  can ask for a <a
  href="reminder.jsp?url=<%=ds.getUrl()%>">reminder</a>.</p>
  </p>

</td>
  </tr>

  <tr>
  <form action="editServer.jsp" method="post">
  	<input type="hidden" name="auto_id" value="<%=auto_id%>"/>
  <td>enter passkey
  	<input type="password" name="passkey" value=""/> 
  	<input type="submit" name="submit" name="Submit Passkey"/>
  </td>
  </tr>
  </table>
  </form> 

  <%@ include file="sangertableend.jsp" %>


  <%
     passkey ="";
  } 
  
  if ( !passkey.equals(""))
      status = bean.validatePasskey(auto_id,passkey);

}
  // status can be:
  //  1: passkey o.k.
  // -1: passkey wrong
  
  if ( isOwner || (status == 1)) {
   	// display all data that can be changed ...	  


%>
<p>
 
 <h2>Edit server <%=ds.getNickname()%></h2>
 
 <form action="updatetestcodes.jsp">
 	<input type="hidden" name="passkey" value="<%=passkey%>"> 
 	<input type="hidden" name="auto_id" value="<%=auto_id%>"/>

	<%
	DasCoordinateSystem[] csses =	ds.getCoordinateSystem()	;
	for (int i =0 ; i< csses.length; i++ ) {
		%><input type="hidden" name="testCodes" value="<%=csses[i].getTestCode()%>"/><% 
	}
	%>
 
<%@ include file="sangertablestart.jsp" %>


 
 	 <%
		int twodays = 1000*60*60*24*2 ;
		Date now = new Date();
		boolean active = true;
		if (ds.getLeaseDate().getTime() < ( now.getTime() - twodays)){
			active = false;
			%>
			<tr id="row0"><td colspan="2">
			<img src="http://www.sanger.ac.uk/icons/new_blue_on_white.gif">
			<b>It has not been possible to validate this server for > 2 days.</b></td></tr>
			<%
		}

	
	String testCode = "";
	DasCoordinateSystem[] coordSys = ds.getCoordinateSystem();
	if ( coordSys.length > 1 )
	   testCode = coordSys[0].getTestCode();
	String url = ds.getUrl();
	String nickname = ds.getNickname();
	
 	 
 	 %>
 	  
	 <tr id="row1">
	     <td>unique id</td>
	     <td><%=ds.getId()%></td>
	 </tr>



 	 <tr id="row2">
	 <td>das source URL</td>
	 <td>

	 <%
	 	  for (int j=0;j<coordSys.length;j++){
		      DasCoordinateSystem dcs = coordSys[j];
		      String org = dcs.getOrganismName();
		      if ( ! org.equals("")) {
			 org = org.replaceAll(" ","_");
			 String thumbnail = "http://www.ensembl.org/img/species/thumb_";			
			 thumbnail += org;
			 thumbnail += ".png";
		      	%><img src="<%=thumbnail%>" valign="bottom"> <%
		      }
		   }
	  %>

	  <input type="text" name="dasurl" value="<%=ds.getUrl()%>" size="40"/></td></tr>




	  <tr id="row1">
	     <td>nickname</td>
	     <td><input type="text" name="nickname" value="<%=ds.getNickname()%>" size="20"/></td>
	 </tr>

    
<%

	 List<String> capabs = ds.getCapabilities();
	 boolean canFeatures = false;	      		   
	 for (int j=0;j<capabs.size();j++){
	       String capability = capabs.get(j);
	       if ( capability.equals("features"))
		  canFeatures = true;
	}
	
	
%>


	<tr id="row2"><td>description</td>
	<td><textarea name="description" COLS="40" ROWS="4" WRAP="hard"><%=ds.getDescription()%></textarea></td></tr>
	
	<tr id="row1"><td>adminemail</td><td><input type="text" name="adminemail" value="<%=ds.getAdminemail()%>"/></td></tr>
	
	
	
	<tr id="row2">
	<td> Owner:
	</td>
	<td>
	<table>
	<%
	
	RegistryUser[] owners = coord.getOwnersForSource(ds);
	
	for (int i =0 ; i< owners.length; i++){
		RegistryUser owner = owners[i];
		%>
		<tr><td><%= owner.getOpenID() %>
		<% 
		if ( user.equals(owner)) {
			%>
			<img src="img/kuser.png"/> <a href="claimSources.jsp?cmd=share&DS_ID=<%=ds.getId() %>"/>share ownership</a>
			<%
		}		
		%>
		</td></tr>
		<%
		
	}		
	%>
	</table>
	</td>
	
	</tr>
	
	<% String helperurl = ds.getHelperurl(); %>
	<tr id="row1"><td>helperurl</td><td>
	<input type="text" name="helperurl" value="<%=helperurl%>"/>	
	</td></tr>
	
	<tr id="row2"> <td> DAS Capabilities: </td>
	 <td>
	 <SELECT NAME="capabilities" size="7" multiple>

		<% 
		  String all_capabilities[] = dasRegistrySql.getAllCapabilities();
		  for (int i=0;i<all_capabilities.length;i++){
		  out.print("<option value=\""+all_capabilities[i]+"\"");
		  List<String> caps = ds.getCapabilities();
		  for ( int j=0; j<caps.size();j++){
		  	String cap = caps.get(j);
			if ( cap.equals(all_capabilities[i])){
				out.print(" selected ");
			}
		  }		  

		  out.print(">"+all_capabilities[i]+"</option>");
		  }	

		  %>
		
		 </SELECT>
	</td></tr>

	<tr id="row2"><td> Test Code: </td>
	  <td>The registry now supports one test code / coordinate system. please specify these on the next page.</td>
	</tr>

	<tr id="row1"><td> Coordinate Systems: </td>
	  <td> 
	  <select  name="CSTypes" multiple size="5">
	  <%

	List<DasCoordinateSystem> coords = dasRegistrySql.getAllCoordinateSystems();
	Comparator dsc = DasCoordSysComparator.fromString("name");
	Collections.sort(coords,dsc);
	
	for ( int i=0; i< coords.size();i++) {
		DasCoordinateSystem dcs = coords.get(i);
		%>
		<option value="<%=dcs.toString()%>" <%
		
		// determine if this coordsys is used ...
		for (int j=0; j< coordSys.length;j++){
		     DasCoordinateSystem mydcs = coordSys[j];		     
		     if ( mydcs.toString().equals(dcs.toString())) {
		      	out.print("selected");
		     }
		}		 
		%>><%=dcs.toString()%></option>
		<%
	}

	%>
	</select> </td></tr>

		 
	<tr id="row1"> 
	<td>Registered at</td> 
	<td><%
		 SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
		 Date d = ds.getRegisterDate() ;
		 out.print(df.format(d));
		 df = null ;
		 d = null ;
		 %>
	</td>
	</tr>
	<tr id="row2"><td>Last tested successfully at: </td>
	<td><%
		 SimpleDateFormat df2 = new SimpleDateFormat("yyyy.MM.dd");
		 Date d2 = ds.getLeaseDate()  ;  
		 out.print(df2.format(d2));
		 df2 = null ;
		 d2 = null ;
		 %>
	</td>
	</tr>

	<tr id="row1">
	<td> label(s)</td>
	<td>
	<select name="labels" multiple size="3" >
	<%
	String[] labels = dasRegistrySql.getAllLabels();
	String[] dslabels = ds.getLabels();
	for (int i=0; i< labels.length; i++ ) {
	%>
		<option value="<%=labels[i]%>" <% 
			for (int j=0;j<dslabels.length;j++){
				String label = dslabels[j];
				if ( label.equals(labels[i])){
					out.print("selected");
				}
			}
		

		%>><%=labels[i]%></option>
	<%
	} 
	%>
	</select>

	</td>
	</tr>

</td></tr>
<tr id="row2">
	<td valign="top">Select the DAS specification this source conforms to</td><td><select name="spec"><option <%if ( ds.getSpecification().equals(DasSpec.SPEC1_53E.toString())){
					out.print("selected");
				}%> value="<%=DasSpec.SPEC1_53E.toString()%>"><%=DasSpec.SPEC1_53E.toString()%></option>
	
	<option <%if ( ds.getSpecification().equals(DasSpec.SPEC1_6E.toString())){
					out.print("selected");
				}%> value="<%=DasSpec.SPEC1_6E.toString() %>"><%=DasSpec.SPEC1_6E.toString() %></option></select></td><td></td></tr>


	<tr id="row1">
	<td> send alert email<br/>
	     when server is down > 2 days.</td>
	<td>
	<input type="checkbox" name="alertAdmin" value="1" <%
	  if ( ds.getAlertAdmin()) {
	 	 out.print(" checked");		 
	 	 updater.setAlertAdmin("0");
	  }
	%>/>
	</td>


	<tr id="row2"> <td>&nbsp;</td> <td align="right"><input type="submit" name="submit" value="update registration"/></td></tr>

</form>

<form name="editAutoActivation" action="editAutoActivationUrl.jsp" method="POST">
<input type="hidden" name="passkey" value="<%=passkey %>"/>
<input type="hidden" name="auto_id" value="<%=auto_id %>"/>
<SCRIPT language="JavaScript">
function submitform()
{
  document.editAutoActivation.submit();
}
</SCRIPT>
	
	
    <tr id="row1"> <td>edit auto activation links</td><td><a href="javascript: submitform()">edit here</a>
	</td>
	</tr>
</form>
</table>

<%@ include file="sangertableend.jsp" %>

<p>

<%

  } else {
    if (( passkey != null )&& (! passkey.equals(""))){
    %>
	<b>wrong key!</b>
	  <%@ include file="editServerLogin.jsp" %>
    <%	
    }
  }
%>

<%@ include file="sangerfooter.jsp" %>


